﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;


namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class UClaimDemageControlList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增, Permission.删除 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UClaimDemageControlList";
            }
        }
        #region 属性 (查询条件)

        protected bool IsJieShan
        {
            get
            {
                return GetRequestValue<bool>("IsJieShan",false);

            }
        }
        protected string CommonFiled
        {
            get
            {
                return GetRequestValue<string>("CommonFiled", "");

            }
        }
        protected string CommonValue
        {
            get
            {
                return GetRequestValue<string>("CommonValue", "");

            }
        }

        protected string ShowAgentName
        {
            get
            {
                return GetRequestValue<string>("ShowAgentName", "");

            }
        }
        protected string DiffDuty
        {
            get
            {
                return GetRequestValue<string>("DiffDuty", "");
            }
        }
      
        protected string DownlineDateFrom
        {
            get
            {
                return GetRequestValue<string>("DownlineDateFrom", "");

            }
        }
        protected string DownlineDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(DownlineDateFrom))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("DownlineDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(DownlineDateFrom).AddDays(1).ToShortDateString();
                    else
                        return todate;
                }

            }
        }
        protected override string orderField
        {
            get
            {
                return string.IsNullOrEmpty(base.orderField) ? "ClaimsNumber" : base.orderField;
            }
        }
        protected override string orderDirection
        {
            get
            {
                return string.IsNullOrEmpty(base.orderDirection) ? "Desc" : base.orderDirection;
            }
        }
        #endregion
        protected DataTable SUMDT = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
            this.AddLog(Permission.查看);
        }

        protected string GetSQLWhere()
        {
            string wheresql = "";
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(CommonValue))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator(CommonFiled, QueryOperatorsEnum.包含, CommonValue);
                wheresql = sqlcommonFiled;
            }

            string DOWNSQL = "";
            if (!string.IsNullOrEmpty(DownlineDateFrom))
            {
                DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("FaultDate", QueryOperatorsEnum.范围, DownlineDateFrom + "|" + DownlineDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }

            string sqlAgentName = "";
            if (!string.IsNullOrEmpty(ShowAgentName))
            {
                sqlAgentName = "'" + ShowAgentName + "'";
                sqlAgentName = sqlAgentName.Replace(",", "','");
                sqlAgentName = " agentname in (" + sqlAgentName + ")";
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlAgentName;
                }
                else
                {
                    wheresql = sqlAgentName;
                }
            }

            string dutySQL = "";
            if (!string.IsNullOrEmpty(DiffDuty))
            {
                dutySQL = QueryOperatorsEnumHelper.ConvertCommonOperator("DiffDuty", QueryOperatorsEnum.包含, DiffDuty);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + dutySQL;
                }
                else
                {
                    wheresql = dutySQL;
                }
            }

            if (IsJieShan)
            {
                dutySQL = " SupplerTotalCostRMB<>FinalTotalCostRMB ";
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + dutySQL;
                }
                else
                {
                    wheresql = dutySQL;
                }
            }

            return wheresql;
        }

        protected void DataBind()
        {



            DataTableCollection dts = UClaimDemageControlController.GetPagination(GetSQLWhere(), pageNum, numPerPage, SQLSort, ref TotalCount);
            
            this.rpMenu.DataSource = dts[0];
            this.rpMenu.DataBind();

            SUMDT = dts[1];
        }


        protected string GetAgentList()
        {
            //DataTable AgentMacStockList = UWarrantyCardController.GetAgentMacNum();

            string litemp = "<li ><a tname=\"{6}\" tvalue=\"{1}\" title=\"全称:[{2}].公司地址:[{3}]\"  style=\"{4}\" {5}>{0}</a></li>";
            string listr = "";
            DataTable dt = StringHelper.GetDTToPinYin(SBOController.GetAgentList(), "AliasName", "PYAliasName", " Asc");
            foreach (DataRow dr in dt.Rows)
            {
                //string x = ShowAgentName == (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString()) ? "color:red;font-weight:bold;" : "";
                int pot = ShowAgentName.IndexOf(string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());
                string x = pot >= 0 ? "color:red;font-weight:bold;" : "";
                string check = pot >= 0 ? "checked='true'" : "";
                // DataRow[] drs = AgentMacStockList.Select("agentname='" + (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"] : dr["AliasName"]) + "'");
                // string agstock = "0";
                //if (drs.Count() > 0)
                //{
                //    agstock = drs[0]["macnum"].ToString();
                //}
                string agentname = (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());

                listr += string.Format(litemp, dr["PYAliasName"].ToString() + agentname, dr["CardCode"], dr["CardName"], dr["Address"], x, check, agentname);
            }
            return listr;
        }

        protected string COldBack(object OInQuantity, object OneedbackQuantity)
        {
            decimal InQuantity = Convert.ToDecimal(OInQuantity);
            decimal needbackQuantity = Convert.ToDecimal(OneedbackQuantity);
            if (needbackQuantity <= 0)
                return "100%";

            return ((InQuantity / needbackQuantity) * 100).ToString("F2") + "%";
        }


        protected string GetUpdateSupplerDetailURL(object ClaimsNumber,object preCost, object nowCost,int urltype)
        {
            /*
              <a class="add" href="UserModel/UClaimDemageControlUpdateSupplerDetail.aspx?ClaimsNumber=<%# Eval("ClaimsNumber") %>" target="dialog" title="查看/修改向供应商索赔明细[索赔单号:<%# Eval("ClaimsNumber") %>]"
                rel="UClaimDemageControlUpdateSupplerDetail" max="false" mask="true" maxable="false" minable="false" resizable="false"
                drawable="true" width="850" height="450">
             */
            decimal _preCost = Convert.ToDecimal(preCost);
            decimal _nowCost = Convert.ToDecimal(nowCost);

            //if (_preCost <=0)
            //    return _nowCost.ToString("F2");

            string opendigurl = " <a  class='btnEditMoney' href=\"UserModel/{0}.aspx?ClaimsNumber={1}\" target=\"navTab\" title=\"[品情单号:{1}]{2}\" rel=\"{0}\" max=\"false\" mask=\"true\" maxable=\"false\" minable=\"false\" resizable=\"false\" drawable=\"true\" width=\"950\" height=\"450\">{3}</a>";
            switch (urltype)
            {
                case 1:
                    opendigurl=string.Format(opendigurl,"UClaimDemageControlUpdateSupplerDetail",ClaimsNumber,"向供应商索赔明细",nowCost);
                    break;
                case 2:
                    opendigurl = string.Format(opendigurl, "UClaimDemageControlUpdateFinalDetail", ClaimsNumber, "索赔结算明细",nowCost);
                    break;
                case 3:
                    opendigurl = string.Format(opendigurl, "UClaimDemageControlDiffAnalyse", ClaimsNumber, "索赔差异分析", nowCost);
                    break;
            }

            return opendigurl;              

        }


        protected string GetSubString(object str,int strlen,string showmoreid)
        {
            string objStr = str.ToString();
            if (objStr.Length <= strlen)
                return objStr;

            return "<a onclick=\"$('#" + showmoreid + "').toggle()\" style=\" cursor:pointer;\" class=\"btnShowMore\">" + objStr.Substring(0, strlen) + "...</a>";

        }

        protected string GetUMDTValue(string filename)
        {
            if (SUMDT.Rows.Count <= 0)
            {
                return "0";
            }
            return SUMDT.Rows[0][filename].ToString();
        }

        protected string GetRedColor(object v)
        {
            if (v is DBNull)
                return "red";

            if(string.IsNullOrEmpty( v.ToString()))
                return "red";

            return "";
        }
    }
}